package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.MemberListBean;
import com.cljz.makepassenger.ui.holder.MemberListHolder;

import java.util.List;

import cn.droidlover.xdroidmvp.kit.Kits;

/**
 * Created by apple on 2017/5/5.
 */

public class MemberListAdapter extends RecyclerView.Adapter<MemberListHolder> {
    private Context context;


    private List<MemberListBean.DataBean> memberList;
    private OnItemClickListener mOnItemClickListener;

    public MemberListAdapter(Context context, List<MemberListBean.DataBean> memberList) {
        this.context = context;
        this.memberList = memberList;
    }

    public void setData(List<MemberListBean.DataBean> memberList) {
        this.memberList = memberList;
        notifyDataSetChanged();
    }

    public void addData(List<MemberListBean.DataBean> memberList) {
        int startPosition = this.memberList.size() + 1;
        this.memberList.addAll(memberList);
        notifyItemChanged(startPosition-1);     //刷新加载前最后一个item所设的padding值
        notifyItemRangeInserted(startPosition, memberList.size());
    }


    @Override
    public MemberListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MemberListHolder(View.inflate(context, R.layout.item_member_list, null));
    }

    @Override
    public void onBindViewHolder(MemberListHolder holder, final int position) {
        holder.setData(memberList, position);
        if (position == memberList.size() -1) {
            holder.itemView.setPadding(0,0,0, Kits.Dimens.dpToPxInt(context,30));
        }else {
            holder.itemView.setPadding(0,0,0,0);
        }
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onClick(memberList.get(position));
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mOnItemClickListener.onLongClick(memberList.get(position));
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return memberList == null ? 0 : memberList.size();
    }

    public interface OnItemClickListener {
        void onClick(MemberListBean.DataBean dataBean);

        void onLongClick(MemberListBean.DataBean dataBean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
